package com.shopping.service.user;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.shopping.constant.ResponseCode;
import com.shopping.dao.UserDao;
import com.shopping.pojo.User;
import com.shopping.util.WXAppletUserUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserDao userDao;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Override
    public Map<String, Object> login(User user, Map<String, Object> wxMap) {
        Map<String, Object> map = new HashMap<>();
        User user1 = userDao.selectByOpenid(user.getOpenid());
        if(user1 == null){
//            Map<String, Object> jsonObject = WXAppletUserUtil.getUserInfo(encryptedData, String.valueOf(wxMap.get("session_key")), iv);
//            if(Integer.parseInt(String.valueOf(jsonObject.get("code"))) == ResponseCode.SERVER_ERROR.getValue()){
//                map.put("code", ResponseCode.SERVER_ERROR.getValue());
//                return map;
//            }
//            JSON json = JSON.parseObject(String.valueOf(jsonObject.get("json")));
//
//            System.out.println("====> json = " + json);

            try{
                userDao.insert(user);
                int userId = userDao.lastInsertId();
                String skey = UUID.randomUUID().toString();
                String redisSkey = stringRedisTemplate.opsForValue().get(user.getOpenid());
                if(StringUtils.isNotBlank(redisSkey)){
                    stringRedisTemplate.delete(user.getOpenid());
                    stringRedisTemplate.delete(redisSkey);
                }
//                JSONObject jsonObject = new JSONObject();
//                jsonObject.put("openid", user.getOpenid());
//                jsonObject.put("session_key", wxMap.get("session_key"));
//                jsonObject.put("userId", userId);
                stringRedisTemplate.opsForValue().set(user.getOpenid(), skey);
//                stringRedisTemplate.opsForValue().set(skey, jsonObject.toJSONString());
                stringRedisTemplate.opsForHash().put(skey, "openid", user.getOpenid());
                stringRedisTemplate.opsForHash().put(skey, "session_key", wxMap.get("session_key"));
                stringRedisTemplate.opsForHash().put(skey, "userId", userId);
                map.put("code", ResponseCode.REQUEST_SUCCESSED.getValue());
                map.put("openid", user.getOpenid());
                map.put("uuid", skey);
            }catch (Exception e){
                map.put("code", ResponseCode.SERVER_ERROR.getValue());
            }
        }
        return map;
    }

    @Override
    public Map<String, Object> update(User user) {
        Map<String,Object> map = new HashMap<>();
        try{
            userDao.updateSelective(user);
            map.put("code",ResponseCode.REQUEST_SUCCESSED.getValue());
        }catch (Exception e){
            map.put("code",ResponseCode.SERVER_ERROR.getValue());
        }
        return map;
    }

    @Override
    public Map<String, Object> selectUserName(int id) {
        Map<String,Object> map = new HashMap<>();
        try{
            String userName = userDao.selectNameById(id);
            if(userName == null){
                map.put("code", ResponseCode.ENTITY_NOT_EXISTS.getValue());
                return map;
            }
            map.put("userName", userName);
            map.put("code",ResponseCode.REQUEST_SUCCESSED.getValue());
        }catch (Exception e){
            map.put("code",ResponseCode.SERVER_ERROR.getValue());
        }
        return map;
    }
}
